package leetcode.code1089;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public void duplicateZeros(int[] arr) {
		int l = 0;
		int cnt = 0;
		int len = arr.length;
		while (l < len && cnt + l < len - 1) {
			cnt += arr[l] == 0 ? 1 : 0;
			l++;
		}
		int r = len - 1;
		if (cnt + l == len - 1)
			arr[r--] = arr[l--];
		else
			l--;
		while (l >= 0) {
			int num = arr[l--];
			if (num == 0) {
				arr[r--] = 0;
				arr[r--] = 0;
			} else {
				arr[r--] = num;
			}
		}
	}

	@Override
	public void debug4() {
		this.test("[1,5,2,0,6,8,0,6,0]");
		this.test("[8,4,5,0,0,0,0,7]");
		this.test("[0,0,0,0,0,0,0]");
		this.test("[0,1,7,6,0,2,0,7]");

	}

	private void test(String str) {
		int[] arr = H.str2array3(str);
		this.duplicateZeros(arr);
		H.print(arr);
	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
